/*
* PickupDropoff Reservation Edit JS
*
* JSLint.com Check:
*/


// functions


//RM.Pages.Functions.PickupDropoff_Reservation_Edit_Save = function(){
//    myMask = new Ext.LoadMask('content-panel', {msg:RM.Translate.Common.PleaseWait});
//    myMask.show();
//    var request = {
//        url : RM.Common.AssembleURL({
//            controller: 'PickupDropoff',
//            action: 'reservationupdatejson',
//            parameters : [
//                {
//                    name : 'id',
//                    value : RM.Pages.Reservations_Edit_ReservationID
//                },{
//                    name : 'location_id_pickup',
//                    value : Ext.getCmp('rm_pickupdropoff_reservation_pickup_combo').getValue()
//                },{
//                    name : 'location_id_dropoff',
//                    value : Ext.getCmp('rm_pickupdropoff_reservation_dropoff_combo').getValue()
//                }
//            ]
//        }),
//        method: 'POST',
//        success: function(responseObject) {
//            myMask.hide();
//        },
//        failure: function() {
//            Ext.Msg.alert('Status', RM.Translate.Common.UnableToShow);
//            myMask.hide();
//        }
//    };
//
//    var conn = new Ext.data.Connection();
//    conn.request(request);
//}



// form objects

//RM.Pages.PickupDropoff_Reservation_Edit_Toolbar = {
//    xtype : "panel",
//    id : "rm_pickupdropoff_reservation_edit_toolbar",
//    bodyBorder : false,
//    html : RM.Common.getToolbar([{image: RM.BaseLargeImageURL+"save.gif", label: RM.Translate.Admin.PickupDropoff.Reservation.Save, link: "RM.Pages.Functions.PickupDropoff_Reservation_Edit_Save()"}])
//}
//RM.Toolbars.push(RM.Pages.PickupDropoff_Reservation_Edit_Toolbar);
//


// new
RM.Pages.Functions.PickupDropoff_ReservationEdit = function(UnitID, FormID, data){

    // define the others systems values (not to be confused with otherinfo)
    // This is used for saving values, simply push in into this array the name
    // of the form objects we need to save
    if (!RM.Common.InArray('PickupDropoff',RM.Pages.Reservations_Edit_OthersSystem_ClassName)){
        RM.Pages.Reservations_Edit_OthersSystem_ClassName.push('PickupDropoff');
        RM.Pages.Reservations_Edit_OthersSystem_FormOjects.push('id_pickup');
        RM.Pages.Reservations_Edit_OthersSystem_FormOjects.push('id_dropoff');
    }

    // load the data for the combo's
    RM.Pages.PickupDropoff_Reservation_Pickup_Locations_Store = new Ext.data.JsonStore({
        fields:[
            {name: "id"},
            {name: "name"}
        ],
        url: RM.Common.AssembleURL({
            controller: 'PickupDropoff',
            action: 'pricelocationsjson'
        })
    });

    RM.Pages.Functions.PickupDropoff_Reservation_Load = function(){

        RM.Pages.PickupDropoff_Reservation_Pickup_Locations_Store.load();

        myMask = new Ext.LoadMask('content-panel', {msg:RM.Translate.Common.PleaseWait});
        myMask.show();
        var request = {
            url : RM.Common.AssembleURL({
                controller: 'PickupDropoff',
                action: 'reservationloadjson',
                parameters : [
                    {
                        name : 'id',
                        value : RM.Pages.Reservations_Edit_ReservationID
                    },{
                        name : 'unit_id',
                        value : UnitID
                    }
                ]
            }),
            method: 'POST',
            success: function(responseObject) {
                var response = Ext.util.JSON.decode(responseObject.responseText);

                Ext.getCmp('id_pickup').setValue(response.id_pickup);
                Ext.getCmp('id_dropoff').setValue(response.id_dropoff);
                myMask.hide();
            },
            failure: function() {
                Ext.Msg.alert('Status', RM.Translate.Common.UnableToShow);
                myMask.hide();
            }
        };

        var conn = new Ext.data.Connection();
        conn.request(request);
    }


    RM.Pages.PickupDropoff_Reservation_Pickup_Combo = {
        xtype: "combo",
        fieldLabel: RM.Translate.Admin.PickupDropoff.Reservation.Pickup,
        id: 'id_pickup',
        hiddenName: 'id_pickup_hidden',
        mode: 'local',
        displayField: "name",
        valueField: "id",
        triggerAction: 'all',
        typeAhead: true,
        resizable: true,
        minListWidth: 220,
        store: RM.Pages.PickupDropoff_Reservation_Pickup_Locations_Store,
        renderer: function(value, metaData, record, rowIndex, colIndex, store) {
            var i = 0;for (i; i < RM.Pages.PickupDropoff_Reservation_Pickup_Locations_Store.data.items.length; i++){
                if (RM.Pages.PickupDropoff_Reservation_Pickup_Locations_Store.data.items[i].data.id === value) {
                    return RM.Pages.PickupDropoff_Reservation_Pickup_Locations_Store.data.items[i].data.name;
                }
            }
            return value;
        }
    };

    RM.Pages.PickupDropoff_Reservation_Dropoff_Combo = {
        xtype: "combo",
        fieldLabel: RM.Translate.Admin.PickupDropoff.Reservation.Dropoff,
        id: 'id_dropoff',
        hiddenName: 'id_dropoff_hidden',
        mode: 'local',
        displayField: "name",
        valueField: "id",
        triggerAction: 'all',
        typeAhead: true,
        resizable: true,
        minListWidth: 220,
        store: RM.Pages.PickupDropoff_Reservation_Pickup_Locations_Store,
        renderer: function(value, metaData, record, rowIndex, colIndex, store) {
            var i = 0;for (i; i < RM.Pages.PickupDropoff_Reservation_Pickup_Locations_Store.data.items.length; i++){
                if (RM.Pages.PickupDropoff_Reservation_Pickup_Locations_Store.data.items[i].data.id === value) {
                    return RM.Pages.PickupDropoff_Reservation_Pickup_Locations_Store.data.items[i].data.name;
                }
            }
            return value;
        }
    };

    return new Ext.form.FieldSet({
        id: FormID+'fieldset',
        bodyStyle : "padding:10px;",
        layout: 'form',
        labelWidth: 130,
        title: RM.Translate.Admin.PickupDropoff.Reservation.SelectedPickUpDropOff,
        height: 100,
        items:[
            RM.Pages.PickupDropoff_Reservation_Pickup_Combo,
            RM.Pages.PickupDropoff_Reservation_Dropoff_Combo
        ],
        listeners: {
            'beforerender' : function(){
                RM.Pages.Functions.PickupDropoff_Reservation_Load();
                return true;
            }
        }
    });
}